package com.dzh.controller;

import com.dzh.pojo.Bill;
import com.dzh.pojo.BillRes;
import com.dzh.pojo.ClassRes;
import com.dzh.pojo.Clazz;
import com.dzh.service.BillService;
import com.dzh.service.ClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/clazz")
public class ClassController {

    @Autowired
    @Qualifier("ClassServiceImpl")
    private ClassService classService;

    /**
     * 查找所有书籍
     * @param model
     * @return
     */
    @RequestMapping("/allClass")
    public String list(Model model) {
        List<ClassRes> list = classService.queryAllClass();
        model.addAttribute("list", list);
        return "allClass";
    }
    /**
     * 去添加书籍
     * @return
     */
    @RequestMapping("/toAddClass")
    public String toAddPaper() {

        return "addClass";
    }

    /**
     * 添加书籍
     * @param clazz
     * @return
     */
    @RequestMapping("/addClass")
    public String addPaper(Clazz clazz) {
        System.out.println(clazz);
        classService.addClass(clazz);
        return "redirect:/clazz/allClass";
    }

    /**
     * 去更新书籍
     * @param model
     * @param classId
     * @return
     */
    @RequestMapping("/toUpdateClass")
    public String toUpdateBill(Model model,@RequestParam("classId") int classId) {
        Clazz clazz = classService.queryClassById(classId);
        model.addAttribute("clazz",clazz );
        return "updateClass";
    }

    /**
     * 更新书籍
     * @param model
     * @param clazz
     * @return
     */
    @RequestMapping("/updateClass")
    public String updateBill(Model model, Clazz clazz) {
        System.out.println(clazz);
        try{
            classService.updateClass(clazz);
        }catch (Exception e){
            e.printStackTrace();
        }
        Clazz class1 = classService.queryClassById(clazz.getClassId());
        model.addAttribute("class", class1);
        return "redirect:/clazz/allClass";
    }

    /**
     * 删除书籍
     * @param classId
     * @return
     */
    @RequestMapping("/del/{classId}")
    public String deleteClass(@PathVariable("classId") int classId) {
        classService.deleteClassById(classId);
        return "redirect:/clazz/allClass";
    }




}